import * as Blockly from 'blockly/core'

const json = {
  type: 'router_redirect',
  message0: '路由跳转 %1 页面名称%2 query参数%3',
  args0: [
    {
      'type': 'input_dummy',
      'name': 'FUNC_INPUT'
    },
    {
      'type': 'input_value',
      'name': 'page_name'
    },
    {
      'type': 'input_value',
      'name': 'page_query',
      'check': ['Object']
    }
  ],
  extensions: ['router_func_extension'],
  previousStatement: true,
  nextStatement: true,
  // inputsInline: true,
  colour: 160,
  tooltip: '',
  helpUrl: ''
  // mutator: 'router_mutator'
}

Blockly.Extensions.register('router_func_extension',
  function () {
    var block = this
    const dropdown = new Blockly.FieldDropdown(
      function () {
        var options = []
        options.push(['路由push跳转', 'push'])
        options.push(['路由replace跳转', 'replace'])
        options.push(['路由返回', 'back'])
        options.push(['关闭当前tab', 'closeView'])
        return options
      })
    dropdown.setValidator(function (value) {
      if (value === 'push' || value === 'replace') {
        if (!block.getInput('page_name')) {
          block.appendValueInput('page_name').appendField('页面名称')
        }
        if (!block.getInput('page_query')) {
          block.appendValueInput('page_query').appendField('query参数')
        }
      } else {
        if (block.getInput('page_name')) {
          block.removeInput('page_name')
        }
        if (block.getInput('page_query')) {
          block.removeInput('page_query')
        }
      }
    })
    // 生成extension的block
    this.getInput('FUNC_INPUT')
      .appendField(dropdown, 'Function')
  })

// 创建积木
Blockly.Blocks['router_redirect'] = {
  init: function () {
    this.jsonInit(json)
  }
}

Blockly.JavaScript['router_redirect'] = function (block) {
  const funcName = block.getFieldValue('Function')
  let code = `context.router().${funcName}(`
  if (block.getInput('page_name')) {
    const pageName = Blockly.JavaScript.valueToCode(block, 'page_name',
      Blockly.JavaScript.ORDER_COMMA) || '\'\''
    code = code + `{title: ${pageName},`
  }
  if (block.getInput('page_query')) {
    const pageQuery = Blockly.JavaScript.valueToCode(block, 'page_query',
      Blockly.JavaScript.ORDER_COMMA) || '{}'
    code = code + `query: ${pageQuery}}`
  }
  code = code + ')\n'
  return code
}

// --------------------------------路由流程页面--------------------------------

const processJson = {
  type: 'router_process',
  message0: '路由跳转流程页面 流程code %1 自定义配置参数 %2',
  args0: [
    {
      'type': 'input_value',
      'name': 'process_code'
    },
    {
      'type': 'input_value',
      'name': 'process_options',
      'check': ['Object']
    }
  ],
  previousStatement: true,
  nextStatement: true,
  // inputsInline: true,
  colour: 160,
  tooltip: '',
  helpUrl: ''
  // mutator: 'router_mutator'
}
// 创建积木
Blockly.Blocks['router_process'] = {
  init: function () {
    this.jsonInit(processJson)
  }
}
Blockly.JavaScript['router_process'] = function (block) {
  let code = `context.router().startProcess(`
  if (block.getInput('process_code')) {
    const processKey = Blockly.JavaScript.valueToCode(block, 'page_name',
      Blockly.JavaScript.ORDER_COMMA) || '\'\''
    code = code + `{processKey: ${processKey},`
  }
  if (block.getInput('process_options')) {
    const options = Blockly.JavaScript.valueToCode(block, 'process_options',
      Blockly.JavaScript.ORDER_COMMA) || `{ title:'',query:{}}`
    code = code + `options: ${options}}`
  }
  code = code + ')\n'
  return code
}

// -------------------------获取路由参数----------------------------------------

const getRouterParamsJson = {
  type: 'router_process',
  message0: '获取路由中的参数对象',
  output: 'Object',
  colour: 160,
  tooltip: '说明用来获取路由中的参数，包含push参数及query参数',
  helpUrl: ''
}
// 创建积木
Blockly.Blocks['router_get_params'] = {
  init: function () {
    this.jsonInit(getRouterParamsJson)
  }
}

Blockly.JavaScript['router_get_params'] = function (block) {
  const code = `context.getRouterParams()`
  return [code, Blockly.Blockly.JavaScript.ORDER_COMMA]
}
